package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        boolean isUpDate = id != null && id != 0;
        saveOrUpdate(roomSubmitVo);

        //删除
        if (isUpDate) {
            LambdaQueryWrapper<RoomLeaseTerm> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(RoomLeaseTerm::getRoomId, id);
            roomLeaseTermService.remove(queryWrapper1);

            LambdaQueryWrapper<RoomPaymentType> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(RoomPaymentType::getRoomId, id);
            roomPaymentTypeService.remove(queryWrapper2);

            LambdaQueryWrapper<RoomAttrValue> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(RoomAttrValue::getRoomId, id);
            roomAttrValueService.remove(queryWrapper3);

            LambdaQueryWrapper<RoomLabel> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(RoomLabel::getRoomId, id);
            roomLabelService.remove(queryWrapper4);

            LambdaQueryWrapper<RoomFacility> queryWrapper5 = new LambdaQueryWrapper<>();
            queryWrapper5.eq(RoomFacility::getRoomId, id);
            roomFacilityService.remove(queryWrapper5);

            LambdaQueryWrapper<GraphInfo> queryWrapper6 = new LambdaQueryWrapper<>();
            queryWrapper6.eq(GraphInfo::getItemId, id);
            queryWrapper6.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(queryWrapper6);

            //保存的新数据
            List<Long> LT = roomSubmitVo.getLeaseTermIds();
            if (!CollectionUtils.isEmpty(LT)) {
                List<RoomLeaseTerm> roomLeaseTermsList = new ArrayList<>(LT.size());
                for (Long termId : LT) {
                    RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(id)
                            .leaseTermId(termId)
                            .build();
                    roomLeaseTermsList.add(roomLeaseTerm);
                }
                roomLeaseTermService.saveBatch(roomLeaseTermsList);
            }

            List<Long> PT = roomSubmitVo.getPaymentTypeIds();
            if (!CollectionUtils.isEmpty(PT)) {
                List<RoomPaymentType> roomPaymentTypesList = new ArrayList<>(PT.size());
                for (Long typeId : PT) {
                    RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(id)
                            .paymentTypeId(typeId)
                            .build();
                    roomPaymentTypesList.add(roomPaymentType);
                }
                roomPaymentTypeService.saveBatch(roomPaymentTypesList);
            }
        }
        List<Long> AV = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(AV)) {
            List<RoomAttrValue> roomAttrValuesList = new ArrayList<>(AV.size());
            for (Long valueId : AV) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(id)
                        .attrValueId(valueId)
                        .build();
                roomAttrValuesList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValuesList);
        }
        List<Long> Label = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(Label)) {
            List<RoomLabel> roomLabelsList = new ArrayList<>(Label.size());
            for (Long labelId : Label) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(id)
                        .labelId(labelId)
                        .build();
                roomLabelsList.add(roomLabel);
            }
            List<Long> Facility = roomSubmitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(Facility)) {
                List<RoomFacility> roomFacilitysList = new ArrayList<>(Facility.size());
                for (Long facilityId : Facility) {
                    RoomFacility roomFacility = RoomFacility.builder().roomId(id)
                            .facilityId(facilityId)
                            .build();
                    roomFacilitysList.add(roomFacility);
                }
                roomFacilityService.saveBatch(roomFacilitysList);
            }
            roomLabelService.saveBatch(roomLabelsList);
        }
        //保存图表数据
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());

            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }

    @Override
    public void removeRoomById(Long id) {
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);


        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
    }
}




